package org.xbzheng.sc.core.aspect;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * Created by Administrator on 2016/7/22.
 */
@Aspect
@Component
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ChooseDataSourceAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(ChooseDataSourceAspect.class);

    @Pointcut("execution(* org.xbzheng.dao.*.*(..))")
    public void aspect(){

    }

    @Before("aspect()")
    public void before(JoinPoint point){
        final String className = point.getTarget().getClass().getName();
        final String methodName = point.getSignature().getName();

        LOGGER.info(className + "." + methodName + "(" + StringUtils.join(point.getArgs(), ",") + ")");

        for(String type: ChooseDataSource.METHOD_TYPE.keySet()){
            final Set<String> methodNames = ChooseDataSource.METHOD_TYPE.get(type);
            if(!methodNames.isEmpty()){
                if(methodNames.contains(methodName)){
                    HandlerDataSource.putDataSource(type);
                }
            }
        }
    }
}
